<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>NSFileManager | NSArray基本概念</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../150.html" />
    
    
    <link rel="prev" href="../nsdate/README.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="12" data-basepath=".." data-revision="1442490111411">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         NSArray基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="nsarray/README.html">
            
                
                    <a href="../nsarray/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         NSArray遍历
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="220.html">
            
                
                    <a href="../220.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         NSArray排序
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="nsarraynsstring/README.html">
            
                
                    <a href="../nsarraynsstring/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         NSArray与NSString
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="230.html">
            
                
                    <a href="../230.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         NSArray文件读写
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="nsmutablearray/README.html">
            
                
                    <a href="../nsmutablearray/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         NSMutableArray基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="nsdictionary/README.html">
            
                
                    <a href="../nsdictionary/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         NSDictionary基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="nsmutabledictionary/README.html">
            
                
                    <a href="../nsmutabledictionary/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         NSMutableDictionary基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="4.html">
            
                
                    <a href="../4.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         常见的结构体
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="nsnumber/README.html">
            
                
                    <a href="../nsnumber/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         NSNumber
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="nsvalue/README.html">
            
                
                    <a href="../nsvalue/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         NSValue
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="nsdate/README.html">
            
                
                    <a href="../nsdate/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         NSDate
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="12" data-path="nsfilemanager/README.html">
            
                
                    <a href="../nsfilemanager/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         NSFileManager
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="13" data-path="150.html">
            
                
                    <a href="../150.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         集合对象的内存管理
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="14" data-path="copy/README.html">
            
                
                    <a href="../copy/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                         Copy
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="15" data-path="888.html">
            
                
                    <a href="../888.html">
                        <i class="fa fa-check"></i>
                        
                            <b>15.</b>
                        
                         copy与内存管理
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="16" data-path="propertycopy/README.html">
            
                
                    <a href="../propertycopy/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>16.</b>
                        
                         @property中的copy关键字
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="17" data-path="567.html">
            
                
                    <a href="../567.html">
                        <i class="fa fa-check"></i>
                        
                            <b>17.</b>
                        
                         自定义的类实现copy操作
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="18" data-path="631.html">
            
                
                    <a href="../631.html">
                        <i class="fa fa-check"></i>
                        
                            <b>18.</b>
                        
                         单例设计模式
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >NSArray基本概念</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_263">
                    
                        <h1 id="nsfilemanager">NSFileManager</h1>
<h2 id="">本小节知识点:</h2>
<ol>
<li>【理解】NSFileManager介绍</li>
<li>【理解】NSFileManager用法(常见的判断)</li>
<li>【理解】NSFileManager的文件访问</li>
<li>【理解】NSFileManager的文件操作</li>
</ol>
<hr>
<h2 id="1nsfilemanager">1.NSFileManager介绍</h2>
<ul>
<li><p>什么是NSFileManager</p>
<ul>
<li>顾名思义, NSFileManager是用来管理文件系统的</li>
<li>它可以用来进行常见的文件\文件夹操作</li>
</ul>
</li>
<li><p>NSFileManager使用了单例模式</p>
<ul>
<li>使用defaultManager方法可以获得那个单例对象<pre><code class="lang-objc">[<span class="hljs-built_in">NSFileManager</span> defaultManager]
</code></pre>
</li>
</ul>
</li>
</ul>
<hr>
<h2 id="2nsfilemanager">2.NSFileManager用法</h2>
<ul>
<li>- (BOOL)fileExistsAtPath:(NSString *)path;<ul>
<li>path这个文件\文件夹是否存在</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">NSFileManager</span> *manager = [<span class="hljs-built_in">NSFileManager</span> defaultManager];
    <span class="hljs-comment">// 可以判断文件</span>
    <span class="hljs-built_in">BOOL</span> flag = [manager fileExistsAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/lnj.txt"</span>];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"flag = %i"</span>, flag);
    <span class="hljs-comment">// 可以判断文件夹</span>
    flag = [manager fileExistsAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/未命名文件夹"</span>];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"flag = %i"</span>, flag);
</code></pre>
<ul>
<li>- (BOOL)fileExistsAtPath:(NSString <em>)path isDirectory:(BOOL </em>)isDirectory;<ul>
<li>path这个文件\文件夹是否存在, isDirectory代表是否为文件夹</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">NSFileManager</span> *manager = [<span class="hljs-built_in">NSFileManager</span> defaultManager];
    <span class="hljs-built_in">BOOL</span> directory = <span class="hljs-literal">NO</span>;
    <span class="hljs-built_in">BOOL</span> flag = [manager fileExistsAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/未命名文件夹"</span> isDirectory:&amp;directory];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"flag = %i, directory = %i"</span>, flag, directory);
</code></pre>
<ul>
<li><p>- (BOOL)isReadableFileAtPath:(NSString *)path;</p>
<ul>
<li>path这个文件\文件夹是否可读</li>
</ul>
</li>
<li><p>- (BOOL)isWritableFileAtPath:(NSString *)path;</p>
<ul>
<li>path这个文件\文件夹是否可写</li>
<li>系统目录不允许写入</li>
</ul>
</li>
<li><p>- (BOOL)isDeletableFileAtPath:(NSString *)path;</p>
<ul>
<li>path这个文件\文件夹是否可删除</li>
<li>系统目录不允许删除</li>
</ul>
</li>
</ul>
<hr>
<h2 id="3nsfilemanager">3.NSFileManager的文件访问</h2>
<ul>
<li>- (NSDictionary <em>)attributesOfItemAtPath:(NSString </em>)path error:(NSError **)error;<ul>
<li>获得path这个文件\文件夹的属性</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">NSFileManager</span> *manager = [<span class="hljs-built_in">NSFileManager</span> defaultManager];
    <span class="hljs-built_in">NSDictionary</span> *dict = [manager attributesOfItemAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/lnj.txt"</span> error:<span class="hljs-literal">nil</span>];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"dit = %@"</span>, dict);
</code></pre>
<ul>
<li><p>- (NSArray <em>)contentsOfDirectoryAtPath:(NSString </em>)path error:(NSError **)error;</p>
<ul>
<li>获得path的当前子路径</li>
</ul>
</li>
<li><p>- (NSData <em>)contentsAtPath:(NSString </em>)path;</p>
<ul>
<li>获得文件内容</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">NSFileManager</span> *manager = [<span class="hljs-built_in">NSFileManager</span> defaultManager];
    <span class="hljs-built_in">NSArray</span> *paths = [manager contentsOfDirectoryAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/"</span> error:<span class="hljs-literal">nil</span>];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"paths = %@"</span>, paths);
</code></pre>
<ul>
<li>- (NSArray <em>)subpathsAtPath:(NSString </em>)path;</li>
<li>- (NSArray <em>)subpathsOfDirectoryAtPath:(NSString </em>)path error:(NSError **)error;<ul>
<li>获得path的所有子路径</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">NSFileManager</span> *manager = [<span class="hljs-built_in">NSFileManager</span> defaultManager];
    <span class="hljs-built_in">NSArray</span> *paths = [manager subpathsAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/"</span>];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"paths = %@"</span>, paths);
</code></pre>
<hr>
<h2 id="4nsfilemanager">4.NSFileManager的文件操作</h2>
<ul>
<li><p>- (BOOL)copyItemAtPath:(NSString <em>)srcPath toPath:(NSString </em>)dstPath error:(NSError **)error;</p>
<ul>
<li>拷贝</li>
</ul>
</li>
<li><p>- (BOOL)moveItemAtPath:(NSString <em>)srcPath toPath:(NSString </em>)dstPath error:(NSError **)error;</p>
<ul>
<li>移动(剪切)</li>
</ul>
</li>
<li><p>- (BOOL)removeItemAtPath:(NSString <em>)path error:(NSError *</em>)error;</p>
<ul>
<li>删除</li>
</ul>
</li>
<li><p>- (BOOL)createDirectoryAtPath:(NSString <em>)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary </em>)attributes error:(NSError **)error;</p>
<ul>
<li>创建文件夹(createIntermediates为YES代表自动创建中间的文件夹)</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">NSFileManager</span> *manager = [<span class="hljs-built_in">NSFileManager</span> defaultManager];
    <span class="hljs-built_in">BOOL</span> flag = [manager createDirectoryAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/test"</span> withIntermediateDirectories:<span class="hljs-literal">YES</span> attributes:<span class="hljs-literal">nil</span> error:<span class="hljs-literal">nil</span>];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"flag = %i"</span>, flag);
</code></pre>
<ul>
<li>- (BOOL)createFileAtPath:(NSString <em>)path contents:(NSData </em>)data attributes:(NSDictionary *)attr;<ul>
<li>创建文件(NSData是用来存储二进制字节数据的)</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">NSString</span> *str = <span class="hljs-string">@"lnj"</span>;
    <span class="hljs-built_in">NSData</span>  *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    <span class="hljs-built_in">NSFileManager</span> *manager = [<span class="hljs-built_in">NSFileManager</span> defaultManager];
    <span class="hljs-built_in">BOOL</span> flag = [manager createFileAtPath:<span class="hljs-string">@"/Users/LNJ/Desktop/abc.txt"</span> contents:data attributes:<span class="hljs-literal">nil</span>];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"flag = %i"</span>, flag);
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../nsdate/README.html" class="navigation navigation-prev " aria-label="Previous page: NSDate"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../150.html" class="navigation navigation-next " aria-label="Next page: 集合对象的内存管理"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
